﻿namespace BorgExt.Behaviors
{
    public abstract class Rule<T> : IRule<T>
    {
        public virtual void AddCandidate(T item)
        { }

        public void Bind(Composer composer)
        {
            Runtime = composer.Runtime;
        }

        public void Bind(Slot slot)
        {
            BehaviorSlot = slot;
            Runtime = slot.Runtime;
        }

        public virtual bool CanCompose(T item, out LogEventArgs log)
        {
            log = null;
            return true;
        }

        public virtual bool CanDecompose(T item, out LogEventArgs log)
        {
            log = null;
            return true;
        }

        public virtual void Composed(T item)
        { }

        public virtual void Composing(T item)
        { }

        public virtual void Decomposed(T item)
        { }

        public virtual void Decomposing(T item)
        { }

        protected virtual void OnCompose(T element)
        {
            if (Compose != null)
                Compose(element);
        }

        protected virtual void OnDecompose(T element)
        {
            if (Decompose != null)
                Decompose(element);
        }

        public virtual void RemoveCandidate(T item)
        { }

        public void Unbind(Composer composer)
        {
            Runtime = null;
        }

        public void Unbind(Slot slot)
        {
            BehaviorSlot = null;
            Runtime = null;
        }

        protected Slot BehaviorSlot { get; set; }

        public EventHandler<T> Compose { get; set; }

        public EventHandler<T> Decompose { get; set; }

        protected Runtime Runtime { get; private set; }
    }
}

